//
//  BDPublishTableViewCell.m
//  BadBoy
//
//  Created by wen on 2017/3/20.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDPublishTableViewCell.h"

@interface BDPublishTableViewCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIImageView *rightImageView;
@property (nonatomic, copy) void(^textBlock)(NSString *text);
@end

@implementation BDPublishTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    style = UITableViewCellStyleValue1;
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
            if (self.textBlock) {
                self.textBlock(self.textField.text);
            }
        }];
    }
    return self;
}

- (void)configeWithDic:(NSDictionary *)dic textFieldBlock:(void(^)(NSString *text))block{
    self.imageView.image = [UIImage imageNamed:dic[@"image"]];
    self.textLabel.text = dic[@"title"];
    self.textField.text = dic[@"subTitle"];
    self.textField.placeholder = dic[@"placeHold"];
    if (dic[@"right"] && self.textField.text.length == 0) {
        self.rightImageView.hidden = NO;
    }else{
        self.rightImageView.hidden = YES;
    }
    if (dic[@"enable"]) {
        self.textField.enabled = NO;
    }else{
        self.textField.enabled = YES;
    }
    if (dic[@"keyoardNumber"]) {
        self.textField.keyboardType = [dic[@"keyoardNumber"] integerValue];
    }else{
        self.textField.keyboardType = UIKeyboardTypeDefault;
    }
    self.textBlock = block;
}

- (void)layoutSubviews{
    self.imageView.size = CGSizeMake(20, 20);
    self.imageView.left = 10;
    self.imageView.centerY = self.height / 2.0;
    
    self.textLabel.font = [UIFont systemFontOfSize:13];
    self.textLabel.textColor = [UIColor colorWithHexString:@"#4c4c4c"];
    self.textLabel.left = self.imageView.right + 6;
    self.textLabel.width = [self.textLabel.text widthWithFont:self.textLabel.font];
    self.textLabel.height = self.height;
    
    self.textField.width = self.width - 10 - self.textLabel.right - 10;
    self.textField.left = self.textLabel.right + 10;
    self.textField.height = 20;
    self.textField.centerY = self.textLabel.centerY;
    self.textField.delegate = self;
    self.textField.font = [UIFont systemFontOfSize:13];
    self.textField.textColor = [UIColor colorWithHexString:@"#4c4c4c"];
    self.textField.tintColor = [UIColor colorWithHexString:@"#cccccc"];
    self.textField.textAlignment = NSTextAlignmentRight;
    self.textField.returnKeyType = UIReturnKeyDone;
    
    self.rightImageView.size = CGSizeMake(10, 10);
    self.rightImageView.right = self.width - 10;
    self.rightImageView.centerY = self.textField.centerY;
    self.rightImageView.image = [UIImage imageNamed:@"righ"];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([self.textLabel.text isEqualToString:ASLocalizedString(@"主题售价")]) {
        //当单独输入小数点的时候，自动补位0
        if (textField.text.length == 0 && [string isEqualToString:@"."]) {
            textField.text = @"0";
            return YES;
        }
        //当输入0的时候，判断当前整数部分是不是为0，如果为0，那么要去掉
        if ([textField.text isEqualToString:@"0"]) {
            textField.text = @"";
            return YES;
        }
        if ([textField.text containsString:@"."]) {
            //如果包含小数点，那么判断小数点小数点后两位
            NSRange pointRange = [textField.text rangeOfString:@"."];
            if (pointRange.location + 3 <= textField.text.length && range.length == 0) {
                return NO;
            }
            //如果已经输入小数点，再输入小数点，返回NO
            if ([string isEqualToString:@"."]) {
                return NO;
            }
        }
    }
    return YES;
}

- (UITextField *)textField{
    if (!_textField) {
        self.textField = [[UITextField alloc] init];
        _textField.delegate = self;
        [self addSubview:_textField];
    }
    return _textField;
}

- (UIImageView *)rightImageView{
    if (!_rightImageView) {
        self.rightImageView = [[UIImageView alloc] init];
        [self addSubview:_rightImageView];
    }
    return _rightImageView;
}

@end
